import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  // 状态/数据 相当于vue的data
  state: {
    goods: []
  },
  // 计算属性
  getters: {
    selectFoods (state) {
      const res = []
      state.goods.forEach(good => {
        good.foods.forEach(food => {
          if (food.count > 0) {
            res.push(food)
          }
        })
      })
      return res
      // console.log(state.goods)
    }
  },
  // 修改state的唯一方式 相当于methods,同步修改
  mutations: {
    SET_GOODS (state, goods) {
      state.goods = goods
    },
    // 修改商品数量
    CHANGE_NUM (state, payload) {
      state.goods.forEach(good => {
        good.foods.forEach(food => {
          if (food.name === payload.food.name) {
            food.count = payload.value
          }
        })
      })
      // console.log(state.goods)
    }
    // 清空购物车
    /*  CLEAR_GOODS (state, selectFoods) {
       console.log(state, selectFoods, 'state,selectFoodsstate,selectFoods')
     } */
  },
  // 异步修改的 methods
  actions: {
  },
  // 把state的数据 分模块管理
  modules: {
  }
})
